﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tasks.Dao;
using Tasks.Common;
using Tasks.Common.Entitys;
using System.Collections;
using MyLib.Util;

namespace Tasks.Business
{
    public class AccountBLL 
    {
        AccountDao accountDao = new AccountDao();
        EmployeeDao employeeDao = new EmployeeDao();
        public AccountBLL()
        {
        }


        #region "账号相关"

        /// <summary>
        /// 创建账号
        /// </summary>
        public ExecuteResult CreateAccount(Account account)
        {
            accountDao.Insert(account);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 修改账号
        /// </summary>
        public ExecuteResult ModifyAccount(Account account)
        {
            accountDao.Update(account);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 获取账号信息
        /// </summary>
        public Account GetAccount(int userID)
        {
            return accountDao.Find(userID);
        }
        public static string GetAccountName(int userID)
        {
            string strUserName = "";
            if (userID != 0)
            {
                AccountDao dao = new AccountDao();
                Account account = dao.Find(userID);
                strUserName = account.Name;
            }
            return strUserName;
        }
        /// <summary>
        /// 获取账号
        /// </summary>
        public Account GetAccountByAccountNo(string AccountNo)
        {
            return accountDao.FindByAccountNo(AccountNo);
        }
        /// <summary>
        /// 搜索手机号码
        /// </summary>
        public Account GetAccountByMobileNo(string MobileNo)
        {
            return accountDao.FindByMobileNo(MobileNo);
        }
        /// <summary>
        /// 搜索账号
        /// </summary>
        public void SearchAccounts(int pageIndex, int pageSize, out int rowCount)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 设置账号状态
        /// </summary>
        public ExecuteResult SetAccountState(int state, int userID)
        {
            new AccountDao().UpdateState(state, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        public ExecuteResult ModifyPassword(string Password, int userID)
        {
            Password = SecurityUtil.EncryptToMD5(Password);

            new EnterpriseMemberDao().UpdatePassWord(Password, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }
        /// <summary>
        /// 保存企业LOGO
        /// </summary>
        /// <param name="logo"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public ExecuteResult SaveLogo(string logo, int userID)
        {
            new EnterpriseMemberDao().UpdateLogo(logo, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "保存成功" };
        }
        #endregion


        #region "员工相关"

        /// <summary>
        /// 创建员工账号
        /// </summary>
        public ExecuteResult CreateEmployee(Employee emp)
        {
            Account account = this.GetAccountByAccountNo(emp.AccountNo);

            //验证用户账号是否重名
            if (account != null)
            {
                return new ExecuteResult() { ResultNo = -1, Message = "账号已存在" };
            }

            emp.Password = SecurityUtil.EncryptToMD5(emp.Password);
            emp.UserType = (int)UserType.Employee;

            new EmployeeDao().InsertALL(emp);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 修改员工资料
        /// </summary>
        public ExecuteResult ModifyEmployee(Employee employee)
        {
            new EmployeeDao().Update(employee);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }


        /// <summary>
        /// 获取员工账号信息
        /// </summary>
        public Employee GetEmployee(int userID)
        {
            return employeeDao.FindByUserID(userID);
        }

        /// <summary>
        /// 设置员工状态
        /// </summary>
        public ExecuteResult SetEmployeeState(EmployeeState state, int employeeID)
        {
            new EmployeeDao().UpdateState((int)state, employeeID);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 搜索员工
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IList<Employee> SearchEmployee(string name,int ownerID, int pageSize, int pageIndex, out int rowCount)
        {
            return this.SearchEmployee(name, 0, "", 0, ownerID, pageSize, pageIndex, out rowCount);
        }

        /// <summary>
        /// 搜索员工
        /// </summary>
        /// <param name="name"></param>
        /// <param name="roleID"></param>
        /// <param name="mobileNo"></param>
        /// <param name="deptID"></param>
        /// <param name="ownerID"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IList<Employee> SearchEmployee(string name, int roleID, string mobileNo, int deptID, int ownerID, int pageSize, int pageIndex, out int rowCount)
        {
            Hashtable condition = new Hashtable();
            condition["RoleID"] = roleID;
            condition["Name"] = name;
            condition["MobileNo"] = mobileNo;
            condition["DeptID"] = deptID;
            condition["OwnerID"] = ownerID;

            return employeeDao.Search(condition, pageSize, pageIndex, out rowCount);
        }

        /// <summary>
        /// 搜索员工
        /// </summary>
        /// <param name="ownerID"></param>
        /// <param name="condition">查询条件为哈希表</param>
        /// <param name="pageSize">每页返回记录数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="rowCount">返回合计记录数</param>
        /// <returns></returns>
        public IList<Employee> SearchEmployee(Hashtable condition,int ownerID, int pageSize, int pageIndex, out int rowCount)
        {
            condition["OwnerID"] = ownerID;
            return employeeDao.Search(condition, pageSize, pageIndex, out rowCount);
        }
        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <returns></returns>
        public IList<Employee> GetEmployees(int ownerID)
        {
            return employeeDao.FindByOwnerID(ownerID);
        }
        #endregion

        #region "企业会员相关"


        /// <summary>
        /// 搜索企业会员
        /// </summary>
        /// <param name="ownerID"></param>
        /// <param name="condition">查询条件为哈希表</param>
        /// <param name="pageSize">每页返回记录数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="rowCount">返回合计记录数</param>
        /// <returns></returns>
        public IList<EnterpriseMember> SearchEnterpriseMember(Hashtable condition, int ownerID,int pageSize, int PageIndex, out int rowCount)
        {
            condition["OwnerID"] = ownerID;
            return new EnterpriseMemberDao().SearchEnterpriseMember(condition, pageSize, PageIndex, out rowCount);

        }

        /// <summary>
        /// 获取企业会员
        /// </summary>
        /// <returns></returns>
        public IList<EnterpriseMember> GetEnterpriseMembers(string name, EnterpriseType entType, int ownerID, int rowCount)
        {
            return new EnterpriseMemberDao().FindAllByCondition(name, entType, ownerID, rowCount);
        }

        /// <summary>
        /// 获取企业会员
        /// </summary>
        /// <returns></returns>
        public IList<EnterpriseMember> GetEnterpriseMembers(string name, int ownerID, int rowCount)
        {
            return new EnterpriseMemberDao().FindAllByCondition(name, null, ownerID, rowCount);
        }

        /// <summary>
        /// 获取企业会员
        /// </summary>
        /// <param name="name"></param>
        /// <param name="entType"></param>
        /// <param name="ownerID"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IList<EnterpriseMember> GetEnterpriseMembers(string name, int[] entType, int ownerID, int rowCount)
        {
            return new EnterpriseMemberDao().FindAllByCondition(name, entType, ownerID, rowCount);
        }

        /// <summary>
        /// 创建企业会员
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public ExecuteResult CreateEnterpriseMember(EnterpriseMember member)
        {
            //没有账号密码，则默认初始一个
            member.AccountNo = string.IsNullOrWhiteSpace(member.AccountNo) ? CommonUtil.GenerateGuid16String() : member.AccountNo;  //随机生成账户
            member.Password = string.IsNullOrWhiteSpace(member.Password) ? new Random().Next(1000000, 9999999).ToString() : member.Password;
            member.Password = SecurityUtil.EncryptToMD5(member.Password);
            member.UserType = (int)UserType.Enterprise;
            //验证
            Account account = this.GetAccountByAccountNo(member.AccountNo);
            if (account != null)
                return new ExecuteResult() { ResultNo = -1, Message = "该账号已存在" };


            new EnterpriseMemberDao().Insert(member);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 修改企业会员资料
        /// </summary>
        public ExecuteResult ModifyEnterpriseMember(EnterpriseMember member)
        {
            new EnterpriseMemberDao().Update(member);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 设置企业会员状态
        /// </summary>
        public ExecuteResult SetEnterpriseMemberState(EnterpriseMemberState state, int userID)
        {
            new EnterpriseMemberDao().UpdateState((int)state, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 获取企业会员信息
        /// </summary>

        public EnterpriseMember GetEnterpriseMember(int UserID)
        {
            return new EnterpriseMemberDao().Find(UserID);
        }

        /// <summary>
        /// 获取企业会员列表
        /// </summary>
        public IList<EnterpriseMember> GetEnterpriseMemberList(int enterpriseType, int OwnerID)
        {
            return new EnterpriseMemberDao().FindByState(OwnerID, enterpriseType);
        }


        #endregion

        #region "个人会员相关"

        /// <summary>
        /// 搜索个人会员
        /// </summary>
        /// <param name="condition">查询条件为哈希表</param>
        /// <param name="pageSize">每页返回记录数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="rowCount">返回合计记录数</param>
        /// <returns></returns>
        public IList<PersonalMember> SearchPersonalMember(Hashtable ht, int OwnerID,int pageSize, int PageIndex, out int rowCount)
        {
            return new PersonalMemberDao().Search(ht, pageSize, PageIndex, out rowCount);
        }

        /// <summary>
        /// 创建个人会员信息
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public ExecuteResult CreatePersonalMember(PersonalMember member)
        {
            member.Password = SecurityUtil.EncryptToMD5(member.Password);
            member.UserType = (int)UserType.Personal;

            new PersonalMemberDao().Insert(member);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 修改个人会员信息
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public ExecuteResult ModifyPersonalMember(PersonalMember member)
        {
            new PersonalMemberDao().Update(member);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 设置个人会员状态
        /// </summary>
        public ExecuteResult SetPersonalMemberState(int state, int userID)
        {
            new PersonalMemberDao().UpdateState(state, userID);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }

        /// <summary>
        /// 获取个人会员信息
        /// </summary>
        public PersonalMember GetPersonalMember(Int32 UserID)
        {
            return new PersonalMemberDao().Find(UserID);
        }

        #endregion

        #region "消息提醒相关"

        /// <summary>
        /// 获取全部消息列表
        /// </summary>
        /// <returns></returns>
        public IList<Notice> NoticeList(Hashtable ht,int ownerID, int pageSize, int PageIndex, out int rowCount)
        {
            ht["OwnerID"] = ownerID;
            return new NoticeDao().Search(ht, pageSize, PageIndex, out rowCount);
        }

        /// <summary>
        /// 获取消息总数
        /// </summary>
        /// <returns></returns>
        public int GetNoticeCount(int OwnerID)
        {
            return new NoticeDao().GetCount(OwnerID);
        }
        /// <summary>
        /// 通过NoticeID和ReaderID查询阅读表
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public int FindByReaderIDNoticeID(Hashtable ht,int ownerID)
        {
            ht["OwnerID"] = ownerID;
            return new NoticeReadStateDao().FindByReaderIDNoticeID(ht);
        }
        /// <summary>
        /// 获取已读消息条数
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public int GetNoticeReadStateByReaderID(int ReaderID, int OwnerID)
        {
            return new NoticeReadStateDao().GetNoticeReadStateByReaderIDCount(ReaderID, OwnerID);
        }
        /// <summary>
        /// 添加消息
        /// </summary>
        /// <param name="notice"></param>
        /// <returns></returns>
        public ExecuteResult AddNotice(Notice notice)
        {
            new NoticeDao().Insert(notice);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }
        /// <summary>
        /// 向阅读表添加数据
        /// </summary>
        /// <param name="Notice"></param>
        /// <returns></returns>
        public ExecuteResult AddReaderState(NoticeReadState Notice)
        {
            new NoticeReadStateDao().Insert(Notice);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }
        /// <summary>
        /// 通过消息ID 获取 地址url
        /// </summary>
        /// <param name="NoticeID"></param>
        /// <returns></returns>
        public Notice GetNoticeByNoticeID(int NoticeID)
        {
            return new NoticeDao().Find(NoticeID);
        }
        /// <summary>
        /// 根据消息类型获取未读消息数量
        /// </summary>
        /// <param name="readerID"></param>
        /// <param name="NoticeMode"></param>
        /// <returns></returns>
        public int GetUnReadeByNoticeModeCount(int readerID, int NoticeMode,int OwnerID)
        {
            Employee employee = GetEmployee(readerID);
            if (employee == null) employee = new Employee();
            return new NoticeReadStateDao().GetUnReadByNoticeModeCount(readerID, NoticeMode, employee.OfficeID, OwnerID);
        }

        public NoticeReadState GetNewNoticeByBoticeMode(int readerID, int NoticeMode, int OwnerID)
        {
            Employee employee = GetEmployee(readerID);
            if (employee == null) employee = new Employee();
            return new NoticeReadStateDao().FindTop1UnReadByNoticeMode(readerID, NoticeMode, employee.OfficeID, OwnerID);
        }

        public void DeteReaderStateByNoticeId(int noticeID)
        {
            new NoticeReadStateDao().DeleteByNoticeID(noticeID);
        }
        #endregion

        public ExecuteResult ModifyNotice(Notice notice)
        {
            new NoticeDao().Update(notice);
            return new ExecuteResult() { ResultNo = 1, Message = "success" };
        }
    }
}
